package com.heima.article.listern;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.mess.ArticleVisitStreamMess;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 监听聚合之后的类
 */
@Component
public class ArticleIncrHandleListener {

    @Resource
    private HotArticleService hotArticleService;

    /**
     * 监听kafka聚合之后的topic
     */
    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void getMessage(String message){
        //1、转化类型
        ArticleVisitStreamMess mess = JSON.parseObject(message, ArticleVisitStreamMess.class);

        //2.执行逻辑业务，重新计算分值，并保存到数据库中和redis中
        hotArticleService.updateScore(mess);
    }
}
